﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace SusuCMS
{
    public class PageType
    {
        public int Id { get; set; }

        [Required]
        [Remote("IdentifierAvailable", "PageType", AdditionalFields = "Id", ErrorMessage = "The Identifier is not available")]
        public string Identifier { get; set; }

        public int? ParentId { get; set; }

        public virtual PageType Parent { get; set; }

        public virtual ICollection<PageType> Children { get; internal set; }

        public virtual ICollection<Page> Pages { get; internal set; }

        [Required]
        public string Name { get; set; }

        public virtual Website Website { get; set; }

        public int WebsiteId { get; set; }

        [Required]
        public string TemplatePath { get; set; }

        public DateTime CreateTime { get; set; }

        public string ExtensionForms { get; set; }

        public bool IsPublished { get; set; }

        public IList<Control> GetControls()
        {
            var list = new List<Control>();
            if (!string.IsNullOrWhiteSpace(this.ExtensionForms))
            {
                var json = new JavaScriptSerializer();
                list = json.Deserialize<List<Control>>(this.ExtensionForms);
            }

            return list;
        }
    }
}
